package io.fredia.femicro.txManager.api.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import io.fredia.femicro.txManager.api.service.ApiTxManagerService;
import io.fredia.femicro.txManager.compensate.model.TransactionCompensateMsg;
import io.fredia.femicro.txManager.compensate.service.CompensateService;
import io.fredia.femicro.txManager.config.ConfigReader;
import io.fredia.femicro.txManager.manager.service.MicroService;
import io.fredia.femicro.txManager.manager.service.TxManagerSenderService;
import io.fredia.femicro.txManager.manager.service.TxManagerService;
import io.fredia.femicro.txManager.model.TxServer;
import io.fredia.femicro.txManager.model.TxState;

/**
 * Created by lorne on 2017/7/1.
 */
@Service
public class ApiTxManagerServiceImpl implements ApiTxManagerService {

	@Autowired
	private TxManagerService managerService;

	@Autowired
	private MicroService eurekaService;

	@Autowired
	private CompensateService compensateService;

	@Autowired
	private TxManagerSenderService txManagerSenderService;

	@Autowired
	private ConfigReader configReader;

	@Override
	public TxServer getServer() {
		return eurekaService.getServer();
	}

	@Override
	public int cleanNotifyTransaction(String groupId, String taskId) {
		return managerService.cleanNotifyTransaction(groupId, taskId);
	}

	@Override
	public boolean sendCompensateMsg(long currentTime, String groupId, String model, String address, String uniqueKey,
			String className, String methodStr, String data, int time, int startError) {
		TransactionCompensateMsg transactionCompensateMsg = new TransactionCompensateMsg(currentTime, groupId, model,
				address, uniqueKey, className, methodStr, data, time, 0, startError);
		return compensateService.saveCompensateMsg(transactionCompensateMsg);
	}

	@Override
	public String sendMsg(String model, String msg) {
		return txManagerSenderService.sendMsg(model, msg, configReader.getTransactionNettyDelayTime());
	}

	@Override
	public TxState getState() {
		return eurekaService.getState();
	}
}
